package com.rzico.sbl.ui.order

import android.widget.ImageView
import androidx.core.view.isVisible
import com.jeremyliao.liveeventbus.LiveEventBus
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.MasterData
import com.rzico.sbl.viewmodel.OrderViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.getExtra
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.glideExt.loadCircleImage
import com.xinnuo.common_ui.utils.showOneHintDialog
import com.xinnuo.slimadapter.SlimAdapter

class OrderShipWhoActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<OrderViewModel>()

    override fun initData() {
        initTitle(title = "配送员列表")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData()
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关配送员信息！"
                }

                recycleList.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData() },
                )

                recycleList.addItemDecoration(SpaceTBDecoration())

                mAdapter = SlimAdapter.creator()
                    .register<MasterData>(R.layout.item_who_list) { jector, bean, _ ->
                        jector
                            .text(R.id.item_who_name, bean.name.orEmpty())
                            .text(R.id.item_who_phone, bean.mobile.orEmpty())
                            .text(R.id.item_who_job, bean.roleName.trimStart('.'))
                            .text(R.id.item_who_shop, bean.shopName)
                            .text(R.id.item_who_ship, bean.shippingCount)
                            .text(
                                R.id.item_who_status,
                                when (bean.status) {
                                    "rest" -> "休息中"
                                    "quit" -> "已离职"
                                    else -> ""
                                }
                            )

                            .with<ImageView>(R.id.item_who_logo) {
                                it.loadCircleImage(bean.logo, R.mipmap.default_user)
                            }

                            .clicked {
                                when (bean.status) {
                                    "rest", "quit" -> {
                                        showOneHintDialog(
                                            hint = "该员工当前不在工作中，请选择其他员工配送"
                                        )
                                    }
                                    else -> {
                                        LiveEventBus
                                            .get("orderShip")
                                            .post("admin,${bean.id},${bean.name},${bean.mobile}")

                                        finishView()
                                    }
                                }
                            }
                    }
                    .attachTo(recycleList)
            }
    }

    /**
     * 配送人员列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .masterList(
                shopId = intent.getExtra("shopId"),
                onSuccess = { list ->
                    mList.clear()
                    list.filter { it.roleId in "1,2,3,10" }
                        .mapTo(mList) { it }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}